package lv.bond.science.nnstudio.ui.imageloader;

import lv.bond.eclipse.imagecanvas.SWTImageCanvas;
import lv.bond.science.nnstudio.model.ImagesBean;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class ImageLoadView extends ViewPart {

	public static final String ID = "lv.bond.science.nnstudio.ui.imageloader.ImageLoadView"; 
	// ID Needs to be whatever is mentioned in plugin.xml

	private Composite top = null;
	private Composite loadTrainDataComposite = null;
	private Button loadTrainDataButton = null;
	private Text trainDataPathText = null;
	private Button loadTrainTargetsDataButton = null;
	private Text trainTargetsDataPathText = null;
	private Button clearTrainDataButton = null;
	private Button clearTrainTargetDataButton = null;
	private Button loadTestDataButton = null;
	private Text testDataPathText = null;
	private Button clearTestDataButton = null;
	private Button loadTestTargetsDataButton = null;
	private Text testTargetsDataPathText = null;
	private Button clearTestTargetDataButton = null;
	private SWTImageCanvas canvas = null;
	
	public ImagesBean model = ImagesBean.getInstance();  //  @jve:decl-index=0:

	@Override
	public void createPartControl(Composite parent) {
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		top = new Composite(parent, SWT.NONE);
		top.setLayout(gridLayout);
		createLoadTrainDataComposite();
		createCanvas();
		
	}

	@Override
	public void setFocus() {
		loadTrainDataButton.setFocus();
	}

	/**
	 * This method initializes loadTrainDataComposite	
	 *
	 */
	private void createLoadTrainDataComposite() {
		GridData gridData11 = new GridData();
		gridData11.horizontalAlignment = GridData.CENTER;
		gridData11.verticalAlignment = GridData.BEGINNING;
		GridData gridData7 = new GridData();
		gridData7.horizontalAlignment = GridData.FILL;
		gridData7.verticalAlignment = GridData.CENTER;
		GridData gridData6 = new GridData();
		gridData6.horizontalAlignment = GridData.FILL;
		gridData6.verticalAlignment = GridData.CENTER;
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = GridData.FILL;
		gridData3.verticalAlignment = GridData.CENTER;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.CENTER;
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = GridData.FILL;
		gridData5.grabExcessHorizontalSpace = true;
		gridData5.verticalAlignment = GridData.CENTER;
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.FILL;
		gridData4.widthHint = -1;
		gridData4.verticalAlignment = GridData.CENTER;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.verticalAlignment = GridData.CENTER;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.widthHint = 120;
		gridData1.verticalAlignment = GridData.CENTER;
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 3;
		loadTrainDataComposite = new Composite(top, SWT.NONE);
		loadTrainDataComposite.setLayout(gridLayout1);
		loadTrainDataComposite.setLayoutData(gridData11);
		loadTrainDataButton = new Button(loadTrainDataComposite, SWT.NONE);
		loadTrainDataButton.setText("Load Train Data");
		loadTrainDataButton.setLayoutData(gridData2);
		loadTrainDataButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						actionLoadTrainData();
					}
				});
		trainDataPathText = new Text(loadTrainDataComposite, SWT.BORDER);
		trainDataPathText.setEditable(false);
		trainDataPathText.setEnabled(true);
		trainDataPathText.setLayoutData(gridData1);
		clearTrainDataButton = new Button(loadTrainDataComposite, SWT.NONE);
		clearTrainDataButton.setText("X");
		clearTrainDataButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						System.out.println("trainData before clear -"+model.getFileNameTrainData());
						actionClearTrainData();
						System.out.println("trainData after clear -"+model.getFileNameTrainData());
						
					}
				});
		loadTrainTargetsDataButton = new Button(loadTrainDataComposite, SWT.NONE);
		loadTrainTargetsDataButton.setText("Load Target Data");
		loadTrainTargetsDataButton.setLayoutData(gridData4);
		loadTrainTargetsDataButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						actionLoadTrainTargetsData();
					}
				});
		trainTargetsDataPathText = new Text(loadTrainDataComposite, SWT.BORDER);
		trainTargetsDataPathText.setEditable(false);
		trainTargetsDataPathText.setEnabled(true);
		trainTargetsDataPathText.setLayoutData(gridData5);
		clearTrainTargetDataButton = new Button(loadTrainDataComposite, SWT.NONE);
		clearTrainTargetDataButton.setText("X");
		loadTestDataButton = new Button(loadTrainDataComposite, SWT.NONE);
		loadTestDataButton.setText("Load Test Data");
		loadTestDataButton.setLayoutData(gridData);
		loadTestDataButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						actionLoadTestData();
					}
				});
		testDataPathText = new Text(loadTrainDataComposite, SWT.BORDER);
		testDataPathText.setEditable(false);
		testDataPathText.setEnabled(true);
		testDataPathText.setLayoutData(gridData6);
		clearTestDataButton = new Button(loadTrainDataComposite, SWT.NONE);
		clearTestDataButton.setText("X");
		clearTestDataButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						actionClearTestData();
					}
				});
		loadTestTargetsDataButton = new Button(loadTrainDataComposite, SWT.NONE);
		loadTestTargetsDataButton.setText("Load Target Data");
		loadTestTargetsDataButton.setLayoutData(gridData3);
		loadTestTargetsDataButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						actionLoadTestTargetsData();
					}
				});
		testTargetsDataPathText = new Text(loadTrainDataComposite, SWT.BORDER);
		testTargetsDataPathText.setEditable(false);
		testTargetsDataPathText.setEnabled(true);
		testTargetsDataPathText.setLayoutData(gridData7);
		clearTestTargetDataButton = new Button(loadTrainDataComposite, SWT.NONE);
		clearTestTargetDataButton.setText("X");
		clearTestTargetDataButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						actionClearTestTargetData();
					}
				});
		clearTrainTargetDataButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						actionClearTrainTargetData();
					}
				});
	}

	public void setTrainDataPathText(String pathName) {
		this.trainDataPathText.setText(pathName);
	}

	public void clearTrainDataPathText() {
		this.trainDataPathText.setText("");
	}

	public void setTrainTargetDataPathText(String pathName) {
		this.trainTargetsDataPathText.setText(pathName);
	}

	public void clearTrainTargetDataPathText() {
		this.trainTargetsDataPathText.setText("");
	}

	public void setTestDataPathText(String pathName) {
		this.testDataPathText.setText(pathName);
	}

	public void clearTestDataPathText() {
		this.testDataPathText.setText("");
	}

	public void setTestTargetDataPathText(String pathName) {
		this.testTargetsDataPathText.setText(pathName);
	}

	public void clearTestTargetDataPathText() {
		this.testTargetsDataPathText.setText("");
	}
	
	public void dispose() {
		super.dispose();
		canvas.dispose();
	}

	/**
	 * This method initializes canvas	
	 *
	 */
	private void createCanvas() {
		GridLayout gridLayout2 = new GridLayout();
		gridLayout2.marginHeight = 5;
		GridData gridData9 = new GridData();
		gridData9.horizontalAlignment = GridData.FILL;
		gridData9.grabExcessHorizontalSpace = true;
		gridData9.grabExcessVerticalSpace = true;
		gridData9.widthHint = 200;
		gridData9.horizontalIndent = 0;
		gridData9.verticalAlignment = GridData.FILL;
		canvas = new SWTImageCanvas(top, SWT.NONE);
		canvas.setLayoutData(gridData9);
		canvas.setLayout(gridLayout2);
	}
	
	// Train
	private void actionLoadTrainTargetsData() {
		canvas.onFileOpen();
		this.trainTargetsDataPathText.setText(
				canvas.getSourceImageFileName() );
		
		model.setFileNameTrainTargetData(canvas.getSourceImageFileName());
	}
	
	private void actionClearTrainTargetData() {
		canvas.clearCanvas();
		this.trainTargetsDataPathText.setText("");
		model.setFileNameTrainTargetData("");
	}
	
	private void actionLoadTrainData() {
		canvas.onFileOpen();
		this.trainDataPathText.setText(
				canvas.getSourceImageFileName() );
		
		model.setFileNameTrainData(canvas.getSourceImageFileName());
	}
	
	private void actionClearTrainData() {
		canvas.clearCanvas();
		this.trainDataPathText.setText("");
		model.setFileNameTrainData("");
	}
	
	// Test
	private void actionLoadTestTargetsData() {
		canvas.onFileOpen();
		this.testTargetsDataPathText.setText(
				canvas.getSourceImageFileName());
		
		model.setFileNameTestTargetData(canvas.getSourceImageFileName());
	}
	
	private void actionClearTestTargetData() {
		canvas.clearCanvas();
		this.trainTargetsDataPathText.setText("");
		model.setFileNameTestTargetData("");
	}
	
	private void actionLoadTestData() {
		canvas.onFileOpen();
		this.testDataPathText.setText(
				canvas.getSourceImageFileName() );
		
		model.setFileNameTestData(canvas.getSourceImageFileName());
	}
	
	private void actionClearTestData() {
		canvas.clearCanvas();
		this.testDataPathText.setText("");
		model.setFileNameTestData("");
	}

}
